{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyOAFEMeJibez6awFaTUv5al",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/madaan/memprompt/blob/main/QueryOpenAI.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!rm -rf prompt-lib\n",
        "!git clone https://github.com/reasoning-machines/prompt-lib\n",
        "!pip install prompt-lib/"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "HQvxP7ZbhTTK",
        "outputId": "c6584ea6-cb8b-4018-ce61-006f8eeff674"
      },
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Cloning into 'prompt-lib'...\n",
            "remote: Enumerating objects: 401, done.\u001b[K\n",
            "remote: Counting objects: 100% (401/401), done.\u001b[K\n",
            "remote: Compressing objects: 100% (225/225), done.\u001b[K\n",
            "remote: Total 401 (delta 232), reused 335 (delta 170), pack-reused 0\u001b[K\n",
            "Receiving objects: 100% (401/401), 2.01 MiB | 18.84 MiB/s, done.\n",
            "Resolving deltas: 100% (232/232), done.\n",
            "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
            "Processing ./prompt-lib\n",
            "\u001b[33m  DEPRECATION: A future pip version will change local packages to be built in-place without first copying to a temporary directory. We recommend you use --use-feature=in-tree-build to test your packages with this new behavior before it becomes the default.\n",
            "   pip 21.3 will remove support for this functionality. You can find discussion regarding this at https://github.com/pypa/pip/issues/7555.\u001b[0m\n",
            "Requirement already satisfied: openai==0.23.0 in /usr/local/lib/python3.7/dist-packages (from prompt-lib==0.1) (0.23.0)\n",
            "Requirement already satisfied: pandas in /usr/local/lib/python3.7/dist-packages (from prompt-lib==0.1) (1.3.5)\n",
            "Requirement already satisfied: tqdm==4.49.0 in /usr/local/lib/python3.7/dist-packages (from prompt-lib==0.1) (4.49.0)\n",
            "Requirement already satisfied: wandb==0.13.2 in /usr/local/lib/python3.7/dist-packages (from prompt-lib==0.1) (0.13.2)\n",
            "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from openai==0.23.0->prompt-lib==0.1) (4.1.1)\n",
            "Requirement already satisfied: pandas-stubs>=1.1.0.11 in /usr/local/lib/python3.7/dist-packages (from openai==0.23.0->prompt-lib==0.1) (1.2.0.62)\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from openai==0.23.0->prompt-lib==0.1) (1.21.6)\n",
            "Requirement already satisfied: requests>=2.20 in /usr/local/lib/python3.7/dist-packages (from openai==0.23.0->prompt-lib==0.1) (2.23.0)\n",
            "Requirement already satisfied: openpyxl>=3.0.7 in /usr/local/lib/python3.7/dist-packages (from openai==0.23.0->prompt-lib==0.1) (3.0.10)\n",
            "Requirement already satisfied: sentry-sdk>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (1.9.0)\n",
            "Requirement already satisfied: protobuf<4.0dev,>=3.12.0 in /usr/local/lib/python3.7/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (3.19.6)\n",
            "Requirement already satisfied: Click!=8.0.0,>=7.0 in /usr/local/lib/python3.7/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (7.1.2)\n",
            "Requirement already satisfied: psutil>=5.0.0 in /usr/local/lib/python3.7/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (5.4.8)\n",
            "Requirement already satisfied: shortuuid>=0.5.0 in /usr/local/lib/python3.7/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (1.0.11)\n",
            "Requirement already satisfied: promise<3,>=2.0 in /usr/local/lib/python3.7/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (2.3)\n",
            "Requirement already satisfied: setproctitle in /usr/local/lib/python3.7/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (1.3.2)\n",
            "Requirement already satisfied: six>=1.13.0 in /usr/local/lib/python3.7/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (1.15.0)\n",
            "Requirement already satisfied: pathtools in /usr/local/lib/python3.7/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (0.1.2)\n",
            "Requirement already satisfied: docker-pycreds>=0.4.0 in /usr/local/lib/python3.7/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (0.4.0)\n",
            "Requirement already satisfied: PyYAML in /usr/local/lib/python3.7/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (6.0)\n",
            "Requirement already satisfied: setuptools in /usr/local/lib/python3.7/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (57.4.0)\n",
            "Requirement already satisfied: GitPython>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from wandb==0.13.2->prompt-lib==0.1) (3.1.29)\n",
            "Requirement already satisfied: gitdb<5,>=4.0.1 in /usr/local/lib/python3.7/dist-packages (from GitPython>=1.0.0->wandb==0.13.2->prompt-lib==0.1) (4.0.10)\n",
            "Requirement already satisfied: smmap<6,>=3.0.1 in /usr/local/lib/python3.7/dist-packages (from gitdb<5,>=4.0.1->GitPython>=1.0.0->wandb==0.13.2->prompt-lib==0.1) (5.0.0)\n",
            "Requirement already satisfied: et-xmlfile in /usr/local/lib/python3.7/dist-packages (from openpyxl>=3.0.7->openai==0.23.0->prompt-lib==0.1) (1.1.0)\n",
            "Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas->prompt-lib==0.1) (2.8.2)\n",
            "Requirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.7/dist-packages (from pandas->prompt-lib==0.1) (2022.6)\n",
            "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20->openai==0.23.0->prompt-lib==0.1) (1.24.3)\n",
            "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20->openai==0.23.0->prompt-lib==0.1) (2.10)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20->openai==0.23.0->prompt-lib==0.1) (2022.9.24)\n",
            "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20->openai==0.23.0->prompt-lib==0.1) (3.0.4)\n",
            "Building wheels for collected packages: prompt-lib\n",
            "  Building wheel for prompt-lib (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for prompt-lib: filename=prompt_lib-0.1-py3-none-any.whl size=40092 sha256=d3726083af2d70d77b9a06f8ef1f2b4ff8a496b6f9e120de9fa6f644d897930d\n",
            "  Stored in directory: /root/.cache/pip/wheels/24/68/12/bf7363e3bd22b539664450d1cffb4fd7d6ad2ac95c3166118e\n",
            "Successfully built prompt-lib\n",
            "Installing collected packages: prompt-lib\n",
            "  Attempting uninstall: prompt-lib\n",
            "    Found existing installation: prompt-lib 0.1\n",
            "    Uninstalling prompt-lib-0.1:\n",
            "      Successfully uninstalled prompt-lib-0.1\n",
            "Successfully installed prompt-lib-0.1\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "BQdqJ5ZmNuLw"
      },
      "outputs": [],
      "source": [
        "import os"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "os.environ[\"OPENAI_API_KEY\"] = 'sk-' #@param {type:\"string\"}\n",
        "!export OPENAI_API_KEY"
      ],
      "metadata": {
        "id": "a39F_ly9hKzy"
      },
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from prompt_lib.backends import openai_api"
      ],
      "metadata": {
        "id": "0G8Z3P7rNwuU"
      },
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "response = openai_api.OpenaiAPIWrapper.call(prompt=\"# function to calculate area of a circle\\n\", engine=\"code-davinci-002\", max_tokens=50, stop_token=\"\\n\\n\", temperature=0.5)"
      ],
      "metadata": {
        "id": "h3lKQJ6TSvuc"
      },
      "execution_count": 11,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "generated_text = openai_api.OpenaiAPIWrapper.parse_response(response)"
      ],
      "metadata": {
        "id": "YTindk1cSxTD"
      },
      "execution_count": 13,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print(generated_text)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "jSJ9gcfhsKrV",
        "outputId": "4d11731d-6cb8-4569-fe41-cfeea7e4668c"
      },
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "def area(r):\n",
            "    return 3.14*r*r\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "3JXZt0g1sWDO"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}